/*
 * Copyright (c) 2010 - 2011, Brookhaven National Laboratory
 * All rights reserved.
 * The software in this package is published under the terms
 * of the BSD license a copy of which has been included with
 * this distribution in the license.txt file.
 */


package org.oxtypes.impl.modifiable;

import java.util.List;
import java.util.ArrayList;

import org.omg.dds.type.dynamic.DynamicType;
import org.omg.dds.type.dynamic.MemberDescriptor;
import org.omg.dds.type.dynamic.modifiable.ModifiableMemberDescriptor;

import org.oxtypes.impl.DDSObjectImpl;

/**
 *
 */
public class ModifiableMemberDescriptorImpl extends DDSObjectImpl
        implements ModifiableMemberDescriptor {

    public ModifiableMemberDescriptorImpl(){
        body = new MemberDescriptorImpl();
    }
    
    // --- From Value: ---------------------------------------------------

    public ModifiableMemberDescriptor modify(){
        modifiable = true;
        return this;
    }

    // --- From ModifiableValue: ------------------------------------

    public ModifiableMemberDescriptor copyFrom(MemberDescriptor other){

        body.type = other.getType();

        body.name  = other.getName();
        body.id    = other.getId();
        body.index = other.getIndex();

        body.defaultValue = other.getDefaultValue();

        body.label = other.getLabel();

        body.defaultLabel = other.isDefaultLabel();

        return this;
    }

    public MemberDescriptor finishModification(){
        modifiable = false;
        return body.clone();
    }

    public ModifiableMemberDescriptor clone(){
        throw new java.lang.UnsupportedOperationException();
    }
    
    // --- From MemberDescriptor: ------------------------------------
    
    public boolean isConsistent() {
        throw new java.lang.UnsupportedOperationException();
    }

    public String getName(){
        return body.name;
    }

    public int getId(){
        return body.id;
    }

    public DynamicType getType(){
        return body.type;
    }

    public String getDefaultValue(){
        return body.defaultValue;
    }

    public int getIndex(){
        return body.index;
    }

    public List<Integer> getLabel(){
        return body.label;
    }

    public boolean isDefaultLabel(){
        return body.isDefaultLabel();
    }
    
    // --- From ModifiableMemberDescriptor: --------------------------

    public ModifiableMemberDescriptor setName(String name){
        if(modifiable) body.name = name;
        return this;
    }

    public ModifiableMemberDescriptor setId(int id){
        if(modifiable) body.id = id;
        return this;
    }

    public ModifiableMemberDescriptor setType(DynamicType type){
        if(modifiable) body.type = type;
        return this;
    }

    public ModifiableMemberDescriptor setDefaultValue(String defaultValue){
        if(modifiable) body.defaultValue = defaultValue;
        return this;
    }

    public ModifiableMemberDescriptor setIndex(int index){
        if(modifiable) body.index = index;
        return this;
    }

    public ModifiableMemberDescriptor setLabel(int... labels){
        if(modifiable == false) return this;
        body.label = new ArrayList<Integer>(labels.length);
        for(int label: labels){
            body.label.add(label);
        }
        return this;
    }

    public ModifiableMemberDescriptor setDefaultLabel(boolean defaultLabel){
        body.defaultLabel = defaultLabel;
        return this;
    }

    private MemberDescriptorImpl body;
    private boolean modifiable = true;

    private class MemberDescriptorImpl extends DDSObjectImpl
            implements MemberDescriptor {

        private MemberDescriptorImpl(){
        }

        // --- From Value: ---------------------------------------------------

        @Override
        public MemberDescriptor clone(){

            MemberDescriptorImpl result = new MemberDescriptorImpl();

            result.type = type;

            result.name  = name;
            result.id    = id;
            result.index = index;

            result.defaultValue = defaultValue;

            if(label != null) {
                result.label = new ArrayList<Integer>(label.size());
                java.util.Collections.copy(result.label, label);
            }

            result.defaultLabel = defaultLabel;

            return result;
        }

        public ModifiableMemberDescriptor modify(){
            return null;
        }

        // --- From MemberDescriptor: ------------------------------------

        public boolean isConsistent() {
            throw new java.lang.UnsupportedOperationException();
        }

        public String getName(){
            return name;
        }

        public int getId(){
            return id;
        }

        public DynamicType getType(){
            return type;
        }

        public String getDefaultValue(){
            return defaultValue;
        }

        public int getIndex(){
            return index;
        }

        public List<Integer> getLabel(){
            if(label == null) return null;
            return java.util.Collections.unmodifiableList(label);
        }

        public boolean isDefaultLabel(){
            return defaultLabel;
        }

        private DynamicType type;

        private String name;
        private int id;
        private int index;

        private String defaultValue;

        private List<Integer> label;
        private boolean defaultLabel;

    }

}
